package testes;

public class TestPat {
	public TestPat ()
	{
	}

	public int pat (char[] subject, char[] pattern)
	{
	// Post: if pattern is not a substring of subject, return -1
//	       else return (zero-based) index where the pattern (first)
//	       starts in subject
if(subject==null||pattern==null||pattern.length==0)
	return -1;
	   final int NOTFOUND = -1;
	   int  iSub = 0, rtnIndex = NOTFOUND;
	   boolean isPat  = false;
	   int subjectLen = subject.length;
	   int patternLen = pattern.length;

	   while (iSub + patternLen <= subjectLen)
	   {
	      if (subject [iSub] == pattern [0])
	      {
	         rtnIndex = iSub; // Starting at zero
	         isPat = true;
	         for (int iPat = 1; iPat < patternLen; iPat ++)
	         {
	            if (subject[iSub + iPat] != pattern[iPat])
	            {
	               rtnIndex = NOTFOUND;
	               isPat = false;
	               break;  // out of for loop
	            }
	         }
	      }
	      iSub ++;
	   }
	   return (rtnIndex);
	}
}
